<template>
  <div>

    <el-card shadow="never" dis-hover class="ivu-mt ">
      <el-row type="flex">
        <el-col v-bind="grid">
          <el-button v-auth="['liveroom-add']" type="primary" icon="md-add" @click="add">添加</el-button>
        </el-col>
      </el-row>
      <el-table
          :data="dataList"
          ref="table"
          class="mt25"
          :loading="loading"
          highlight-current-row
      >
        <el-table-column prop="title" label="标题">
        </el-table-column>
        <el-table-column prop="introduce" label="介绍">
        </el-table-column>
        <el-table-column prop="begin_time" label="开始时间">
        </el-table-column>
<!--        <el-table-column prop="end_time" label="结束时间">-->
<!--        </el-table-column>-->
        <el-table-column prop="status_label" label="状态">
        </el-table-column>
<el-table-column label="封面">
   <template slot-scope="scope">
      <div class="tabBox_img" v-viewer>
        <img v-lazy="scope.row.cover" />
      </div>
  </template>
</el-table-column>

        <el-table-column label="操作">
            <template slot-scope="scope">
              <a @click="show(scope.row)">详情</a>
              <el-divider direction="vertical" />
              <a @click="edit(scope.row.id)">修改</a>
              <el-divider direction="vertical" />
              <a @click="del(scope.row, '删除', scope.$index)">删除</a>
            </template>
        </el-table-column>
      </el-table>
      <div class="acea-row row-right page">
        <pagination :total="total"  @pagination="pageChange"  :limit.sync="from.limit" :page.sync="from.page" />
      </div>
    </el-card>

    <el-dialog title="查看详情" :visible.sync="dialogTableVisible" v-if='dialogTableVisible'>
        <el-descriptions title="liveroom">
            <el-descriptions-item label="自增ID">{{info.id}}</el-descriptions-item>
            <el-descriptions-item label="用户ID">{{info.user_id}}</el-descriptions-item>
            <el-descriptions-item label="标题">{{info.title}}</el-descriptions-item>
            <el-descriptions-item label="介绍">{{info.introduce}}</el-descriptions-item>
            <el-descriptions-item label="开始时间">{{info.begin_time}}</el-descriptions-item>
<!--            <el-descriptions-item label="结束时间">{{info.end_time}}</el-descriptions-item>-->
            <el-descriptions-item label="状态">{{info.status_label}}</el-descriptions-item>
            <el-descriptions-item label="封面"><el-image :src="info.cover" :preview-src-list="info.cover"/></el-descriptions-item>
            <el-descriptions-item label="添加时间">{{info.create_time}}</el-descriptions-item>
            <el-descriptions-item label="修改时间">{{info.update_time}}</el-descriptions-item>

        </el-descriptions>
    </el-dialog>

  </div>
</template>

<script>
import { mapState } from 'vuex';
import { liveroomSaveApi, liveroomStatusApi, liveroomDeleteApi, liveroomUpdateApi, getLiveroomCreateApi, getLiveroomEditApi, getLiveroomListApi, getLiveroomReadApi} from '@/api/crud/liveroom';
export default {
  name: 'liveroom',
  data() {
    return {
      grid: {
        xl: 7,
        lg: 7,
        md: 12,
        sm: 24,
        xs: 24,
      },
      loading: false,
      from: {
        page: 1,
        limit: 15,
      },
      dataList: [],
      total: 0,
      dialogTableVisible: false,
      info: {},
    };
  },
  computed: {
    ...mapState('media', ['isMobile']),
    labelWidth() {
      return this.isMobile ? undefined : '75px';
    },
    labelPosition() {
      return this.isMobile ? 'top' : 'left';
    },
  },
  created() {
    this.getList();
  },
  methods: {
    show(row) {
        getLiveroomReadApi(row.id).then(res => {
           this.dialogTableVisible = true;
           this.info = res.data;
        }).catch(res => {
          this.$Message.error(res.msg);
        })
    },
    //修改状态
    updateStatus(row, field) {
        liveroomStatusApi(row.id, {field: field, value: row[field]})
        .then(async (res) => {
          this.$message.success(res.msg);
        })
        .catch((res) => {
          this.$message.error(res.msg);
        });
    },
    // 添加
    add() {
      this.$modalForm(getLiveroomCreateApi()).then(() => this.getList());
    },
    // 表格搜索
    searchs() {
      this.from.page = 1;
      this.getList();
    },
    //列表
    getList() {
      this.loading = true;
      getLiveroomListApi(this.from)
          .then(async (res) => {
            let data = res.data;
            this.dataList = data.list;
            this.total = data.count;
            this.loading = false;
          })
          .catch((res) => {
            this.loading = false;
            this.$Message.error(res.msg);
          });
    },
    //分页
    pageChange(index) {
      this.from.page = index;
      this.getList();
    },
    // 修改
    edit(id) {
      this.$modalForm(getLiveroomEditApi(id)).then(() => this.getList());
    },
    // 删除
    del(row, tit, num) {
      let delfromData = {
        title: tit,
        num: num,
        url: `crud/liveroom/${row.id}`,
        method: 'DELETE',
        ids: '',
      };
      this.$modalSure(delfromData)
          .then((res) => {
            this.$message.success(res.msg);
            this.getList();
          })
          .catch((res) => {
            this.$message.error(res.msg);
          });
    },
  },
};
</script>

<style scoped lang="stylus"></style>
